package P3排序算法;

import java.util.Arrays;

//归并排序 O(n*logn) S(n) 稳定
//最核心的用处是在外部排序
/*
比较类 vs 非比较类
稳定 vs 不稳定
简单 vs 高级
内部(所有数据在内存中排序) vs 外部(部分数据需要临时存储到硬盘进行排序)
排序1T数据
*/
public class MergeSort {
    public static void main(String[] args) {
//        combine01();
//        combine02();
        int[] arr = {1, 2, 6, 8, 9, 2, 2, 6, 7, 8, 8, 9, 9};
        sort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr, int L, int R) {
        if (L >= R) {
            return;
        }
        int M = (L + R) / 2;
        //拆分递归
        sort(arr, L, M);    //左边递归
        sort(arr, M + 1, R);    //右边递归
        //左右都有序的话 合并
        if (arr[M] > arr[M + 1]) {
            merge(arr, L, M, R);
        }
    }

    private static void merge(int[] arr, int L, int M, int R) {
        int[] aux = new int[R - L + 1];
        for (int i = 0; i < aux.length; i++) {
            aux[i] = arr[i + L];
        }
        int l1 = 0, r1 = M - L, l2 = r1 + 1, r2 = R - L;
        int p1 = l1, p2 = l2, p3 = L;
        while (p1 <= r1 || p2 <= r2) {
            if (p1 > r1) {
                arr[p3] = aux[p2];
                p2++;
                p3++;
            } else if (p2 > r2) {
                arr[p3] = aux[p1];
                p1++;
                p3++;
            } else if (aux[p1] <= aux[p2]) {
                arr[p3] = aux[p1];
                p1++;
                p3++;
            } else {
                arr[p3] = aux[p2];
                p2++;
                p3++;
            }
        }
    }

    private static void combine02() {
        int[] arr = {1, 2, 6, 8, 9, 2, 2, 6, 7, 8, 8, 9, 9};
        int l1 = 0, r1 = 4, l2 = 5, r2 = arr.length - 1;
        int[] temp = new int[arr.length];
        int p1 = l1, p2 = l2, p3 = 0;
        while (p1 <= r1 || p2 <= r2) {
            if (p1 > r1) {
                temp[p3] = arr[p2];
                p2++;
                p3++;
            } else if (p2 > r2) {
                temp[p3] = arr[p1];
                p1++;
                p3++;
            } else if (arr[p1] <= arr[p2]) {
                temp[p3] = arr[p1];
                p1++;
                p3++;
            } else {
                temp[p3] = arr[p2];
                p2++;
                p3++;
            }
        }
        System.out.println(Arrays.toString(temp));
        System.out.println(Arrays.toString(arr));
    }

    private static void combine01() {
        int[] arr1 = {1, 2, 6, 8, 9};
        int[] arr2 = {2, 2, 6, 7, 8, 8, 9, 9};
        int[] arr3 = new int[arr1.length + arr2.length];
        int p1 = 0, p2 = 0, p3 = 0;
        while (p1 < arr1.length || p2 < arr2.length) {
            if (p1 >= arr1.length) {
                arr3[p3] = arr2[p2];
                p2++;
                p3++;
            } else if (p2 >= arr2.length) {
                arr3[p3] = arr1[p1];
                p1++;
                p3++;
            } else if (arr1[p1] <= arr2[p2]) {
                arr3[p3] = arr1[p1];
                p1++;
                p3++;
            } else {
                arr3[p3] = arr2[p2];
                p2++;
                p3++;
            }
        }
        System.out.println(Arrays.toString(arr3));
    }
}
